package service.configuration.xstream.converter;

import service.configuration.xstream.LinkAssociationUpConfig;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;


public class LinkUpConverter implements Converter {

	@Override
	@SuppressWarnings("rawtypes")
	public boolean canConvert(Class clazz) {
		return clazz.equals(LinkAssociationUpConfig.class);
	}

	@Override
	public void marshal(Object o, HierarchicalStreamWriter h,
			MarshallingContext m) {
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		LinkAssociationUpConfig o = new LinkAssociationUpConfig();
		o.setTo(reader.getAttribute("to"));
		while(reader.hasMoreChildren()) {
			reader.moveDown();
			if (reader.getNodeName().equals("label")) {
				o.setLabel(reader.getValue());
			} else if (reader.getNodeName().equals("query")) {
				o.setQuery(reader.getValue());
			}
			reader.moveUp();
		}
		return o;
	}

}
